package com.hxzy.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hxzy.common.annotation.Log;
import com.hxzy.common.annotation.RepeatSubmit;
import com.hxzy.common.core.controller.BaseController;
import com.hxzy.common.core.domain.R;
import com.hxzy.common.core.domain.entity.SysDictData;
import com.hxzy.common.enums.BusinessType;
import com.hxzy.common.utils.StringUtils;
import com.hxzy.common.utils.poi.ExcelUtil;
import com.hxzy.system.domain.dto.SysDictDataSearchDTO;
import com.hxzy.system.service.ISysDictDataService;
import com.hxzy.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "数据字典值")
@RestController
@RequestMapping(value = "/api/system/dict/data")
public class SysDictDataController extends BaseController {


    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;




    @ApiOperation(value = "分页查询")
    @PreAuthorize("@hxzy.hasPermi('system:dict:list')")
    @GetMapping("/list")
    public R list(SysDictDataSearchDTO sysDictDataSearchDTO)
    {
        IPage<SysDictData> page = dictDataService.search(sysDictDataSearchDTO);
        return  super.pageVO(page);
    }



    @ApiOperation(value = "导出excel")
    @PreAuthorize("@hxzy.hasPermi('system:dict:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictDataSearchDTO sysDictDataSearchDTO)
    {
        List<SysDictData> list = dictDataService.searchExport(sysDictDataSearchDTO);
        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
        util.exportExcel(response, list, "字典数据");
    }


    @ApiOperation(value = "查询字典数据详细")
    @PreAuthorize("@hxzy.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dictCode}")
    public R getInfo(@PathVariable Long dictCode)
    {
        SysDictData sysDictData=this.dictDataService.getById(dictCode);
        sysDictData.setUpdateBy(null);
        sysDictData.setUpdateTime(null);
        return R.okHasData(sysDictData);
    }


    @PreAuthorize("@hxzy.hasPermi('system:dict:list')")
    @ApiOperation(value = "根据字典类型查询字典数据信息")
    @GetMapping(value = "/type/{dictType}")
    public R dictType(@PathVariable String dictType)
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<SysDictData>();
        }
        return R.okHasData(data);
    }

    @ApiOperation(value = "新增字典类型")
    @PreAuthorize("@hxzy.hasPermi('system:dict:add')")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@Validated @RequestBody SysDictData dict)
    {
        return toAjax(this.dictDataService.save(dict));
    }


    @ApiOperation(value = "修改保存字典类型")
    @PreAuthorize("@hxzy.hasPermi('system:dict:edit')")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@Validated @RequestBody SysDictData dict)
    {
        return toAjax(dictDataService.updateById(dict));
    }

    @ApiOperation(value = "删除字典类型")
    @PreAuthorize("@hxzy.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public R remove(@PathVariable Long[] dictCodes)
    {
        dictDataService.deleteDictDataByIds(dictCodes);
        return R.ok();
    }

}
